package com.zhl.study.controller;

import com.zhl.study.base.ResponseFactory;
import com.zhl.study.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description spring-stop-watch优雅的打印耗时
 * @Author: z156940l@outlook.com
 * @className TestControlller
 * @Date: 2023/4/28 16:51
 */
@Slf4j
@RestController
@RequestMapping("api/")
@Api(value = "spring-stop-watch优雅的打印耗时", tags = "spring-stop-watch优雅的打印耗时")
public class TestControlller {

    @GetMapping("/putong")
    @ApiOperation(httpMethod = "GET", value = "普通打印")
    public Result<Void> putong() throws InterruptedException {
        long start = System.currentTimeMillis();
        Thread.sleep(100);
        long end = System.currentTimeMillis();
        log.info("耗时:{}", end -start);
        return ResponseFactory.ok();
    }

    @GetMapping("/stopWatchPt")
    @ApiOperation(httpMethod = "GET", value = "stopWatch普通打印")
    public Result<Void> stopWatchPt() throws InterruptedException {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Thread.sleep(1000);
        stopWatch.stop();
        log.info("耗时:{}", stopWatch.getTotalTimeMillis());
        return ResponseFactory.ok();
    }

    @GetMapping("/stopWatchFz")
    @ApiOperation(httpMethod = "GET", value = "stopWatch复杂打印")
    public Result<Void> stopWatchFz() throws InterruptedException {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("A");
        Thread.sleep(1000);
        stopWatch.stop();
        stopWatch.start("B");
        Thread.sleep(1000);
        stopWatch.stop();
        stopWatch.start("C");
        Thread.sleep(1000);
        stopWatch.stop();
        log.info("耗时:{}", stopWatch.prettyPrint());
        return ResponseFactory.ok();
    }
}
